const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/yxlm', { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => {console.log('数据库连接成功')})
.catch(err => {console.log(err + '数据库连接失败')});


// user集合规则
const userSchema = new mongoose.Schema({
    name: String
});
// post集合规则
const postSchema = new mongoose.Schema({
    title: String,
    auther: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    }
});

// 创建User集合
const User = mongoose.model('User', userSchema);
// 创建Post集合
const Post = mongoose.model('Post', postSchema);


// 创建User集合对象
// User.create({name: '鲁迅'}).then(() => {console.log('User集合对象创建成功')});
// 创建Post集合对象
// Post.create({title: '狂人日屄', auther: '5f0d44d60561b81dec8612f5'}).then(() => {console.log('Post集合对象创建成功')});

Post.find().populate('auther').then(doc => {console.log(doc)});